package com.flower.core_service.controller;

import org.springframework.beans.BeansException;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("system")
public class SystemController implements ApplicationContextAware {
    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }

    @GetMapping("/shutDown")
    public void shutDown(){
        /*方式1 */
        //ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context).getEnvironment();

        /*方式2*/
        //获取退出码
        int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
        //退出码传递给jvm，安全退出程序
        System.exit(exitCode);
    }
}
